#include "StdAfx.h"
#include "Permutation.h"


Permutation::Permutation(unsigned long *permutationList,int wordLen = 1, int groupSize = 1): m_WordLen(wordLen), m_GroupSize(groupSize), m_Permutation(permutationList)
{}


Permutation::~Permutation(void)
{
}


//TODO: add checks for array size
// the methods receives  int arrays and counts how many elements are in the intersection between 
// it and the current perm
// Note: the method assumes the arrays are unique (do not have repetitions). If the arrays have 
// repetitions the output is undefined.
// groupB is assumed to be same size as current perm
int Permutation::CountIntersection(unsigned long* groupB)
{
	int intersectionSize = 0;
	for (int i=0; i<m_GroupSize; i++)
	{
		for (int j=0; j<m_GroupSize; j++)
		{
			if (m_Permutation[i]==groupB[j])
			{
				intersectionSize++;
			}
		}
	}
	return intersectionSize;
}

unsigned long *Permutation::GetMatrixColumns() const
{
	return TransposeMatrixBits(m_GroupSize,m_WordLen);
}

unsigned long *Permutation::GetMatrixRows() const
{
	return m_Permutation;
}

int Permutation::GetGroupSize() const
{
	return m_GroupSize;
}

// Parameters:
// rowCount
// colCount
// Returns:
// New int array, 1  int for each column. Leftmost column is 1st int.
unsigned long* Permutation::TransposeMatrixBits(const int rowCount, const int colCount) const
{
	unsigned long *transMatrix = new unsigned long[colCount];
	
	for (int i = 0; i < colCount ; i++)
	{
		transMatrix[i] = 0;
	}

	// Go over each one of the numbers
	for (int i = 0; i < rowCount; i++)
	{
		int currentNumber = m_Permutation[i];
		// and place its bits in the right places in the column array
		for (int j = colCount-1 ; j >= 0 ; j--)
		{
			int currentBit = currentNumber & 1; // Get last bit
			currentNumber = currentNumber >> 1; // Shift right to deal with next bit
			transMatrix[j] = (transMatrix[j] << 1)+currentBit; // Shift the previous bit to the left (it is a left-er column) and add
		}
	}
	return transMatrix;
}

